package com.lzhsite.technology.designPattern.adapter.Address;

public class Customer
{
    public static final String US = "US";
    public static final String CANADA = "CANADA";

    private String address;
    private String name;
    private String zip;
    private String state;
    private String type;

    public Customer(String address, String name, String zip, String state, String type)
    {
        this.address = address;
        this.name = name;
        this.zip = zip;
        this.state = state;
        this.type = type;
    }

    public boolean isValidAddress()
    {
    	//Customer期望能通过调用AddressValidator接口isValidAddress方法，从而实现调用USAddress/CAAddress类的验证方法(实现不同验证器的统一调度)。
        //CAAddress是原来老系统里就存在的类,不能把方法名isValidCanadianAddress直接改为isValidAddress。否者会影响老系统的使用
    	//这时就需要一个适配器CAAddressAdapter继承CAAddress，并且实现接口isValidAddress
    	//Customer的里根据入参来判断是实例化CAAddressAdapter还是USAddress
    	AddressValidator addressValidator = getValidator(type);
        return addressValidator.isValidAddress(address, zip, state);
    }

    private AddressValidator getValidator(String customerType)
    {
        AddressValidator addressValidator = null;
        if (customerType.equals(Customer.US))
        {
            addressValidator = new USAddress();
        }
        if (customerType.equals(Customer.CANADA))
        {
            addressValidator = new CAAddressAdapter();
        }
        return addressValidator;
    }
}
